package com.dbq.fast.analysis;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.firebase.analytics.FirebaseAnalytics;

import java.util.HashMap;
import java.util.Map;

import www.yiba.com.analytics.core.AnalyticManager;
import www.yiba.com.analytics.core.EventCall;
import www.yiba.com.analytics.core.URLManager;
import www.yiba.com.analytics.core.YibaAppAgent;
import www.yiba.com.analytics.core.YibaEvent;


/**
 * Created by yiba_zyj on 2017/11/8.
 */

public class YibaAnalysis {

    private static volatile YibaAnalysis analysis = null;

    //app 时长统计
    public String URL_APP_AGENT = "https://wifitj.kuwifi.net/sdk-wifi/vpn/unicornRetention";

    //APP事件统计
    public String URL_EVENT = "https://wifitj.pegasus-mobile.com/sdk-wifi/vpn/addUnicornVpnEvent";

    //APP用户活跃
    public String URL_ACTIVE = "https://wifitj.kuwifi.net/sdk-wifi/vpn/addUnicornActiveUser";

    private YibaAnalysis() {
    }

    public static YibaAnalysis getInstance() {
        if (analysis == null) {
            synchronized (YibaAnalysis.class) {
                if (analysis == null) {
                    analysis = new YibaAnalysis();
                }
            }
        }
        return analysis;
    }

    /**
     * 统计模块初始化
     */
    public void init(Context context, Boolean openLog, int maxCache, final EventCallBack eventCall) {
        //自己的服务器
//        AnalyticManager.initAnalytics(context, openLog, maxCache,  new EventCall() {
//            @Override
//            public void event(String s) {
//                eventCall.event(s);
//            }
//
//            @Override
//            public void pageInterval(String s) {
//                eventCall.pageInterval(s);
//            }
//
//            @Override
//            public void active(String s) {
//                eventCall.active(s);
//            }
//        });

        //设置请求url
        URLManager urlManager = new URLManager();
        urlManager.setEventUrl(URL_EVENT);  //设置事件埋点统计url
        urlManager.setActiveUrl(URL_ACTIVE); //设置活跃统计url
        urlManager.setAgentUrl(URL_APP_AGENT); //设置app页面停留时长统计url

        //统计初始化
        AnalyticManager.initAnalytics(context, openLog, maxCache, urlManager, new EventCall() {
            @Override
            public void event(String s) {
                eventCall.event(s);
            }

            @Override
            public void pageInterval(String s) {
                eventCall.pageInterval(s);
            }

            @Override
            public void active(String s) {
                eventCall.active(s);
            }
        });
    }

    /**
     * event事件
     */
    public void event(Context context, String event, Bundle bundle) {
        if (context == null) return;
        if (TextUtils.isEmpty(event)) return;
        //firebase
        eventFirebase(context, event, bundle);

        //自己的服务器
//        YibaEvent.getInstance().event(context, event);

        //定义一个埋点数据
        Map<String, Object> map = new HashMap<>();
        map.put("eventName", event);
        map.put("timestamp", ""+System.currentTimeMillis());
        map.put("createTime", TimeUtil.getCurrentTime5());
        map.put("dateZone", TimeUtil.getCurrentTimeBeijing());
        if(bundle != null) {
            for (String key : bundle.keySet()) {
//                Log.e("yc_", "bundle_key:"+bundle.getString(key));
                map.put("segment", bundle.getString(key));
                break;
            }
        }

        //上报埋点数据
        YibaEvent.getInstance().event(context, map);

    }

    /**
     * event事件
     */
    public void event(Context context, String event) {
        if (context == null) return;
        if (TextUtils.isEmpty(event)) return;
        //firebase
        eventFirebase(context, event);

        //自己的服务器
//        YibaEvent.getInstance().event(context, event);

        //定义一个埋点数据
        Map<String, Object> map = new HashMap<>();
        map.put("eventName", event);
        map.put("timestamp", ""+System.currentTimeMillis());
        map.put("createTime", TimeUtil.getCurrentTime5());
        map.put("dateZone", TimeUtil.getCurrentTimeBeijing());

        //上报埋点数据
        YibaEvent.getInstance().event(context, map);
    }

    /**
     * firebase的统计事件，event事件可以传中文
     */
    public void eventFirebase(Context context, String event) {
        Bundle bundle = new Bundle();
        bundle.putString(FirebaseAnalytics.Param.ITEM_ID, event);
        bundle.putString(FirebaseAnalytics.Param.LOCATION, context.getResources().getConfiguration().locale.getCountry());
        bundle.putString("version", getAppVersion(context));

        getmFirebaseAnalytics(context).logEvent(event, bundle);
    }

    /**
     * firebase的统计事件，event事件可以传中文
     */
    public void eventFirebase(Context context, String event, Bundle bundle) {
        bundle.putString(FirebaseAnalytics.Param.ITEM_ID, event);
        bundle.putString(FirebaseAnalytics.Param.LOCATION, context.getResources().getConfiguration().locale.getCountry());
        bundle.putString("version", getAppVersion(context));

        getmFirebaseAnalytics(context).logEvent(event, bundle);

    }

    /**
     * yiba事件统计
     */
    public void eventYiba(Context context, String event) {
//        YibaEvent.getInstance().event(context, event);

        //定义一个埋点数据
        Map<String, Object> map = new HashMap<>();
        map.put("eventName", event);
        map.put("timestamp", ""+System.currentTimeMillis());
        map.put("createTime", TimeUtil.getCurrentTime5());
        map.put("dateZone", TimeUtil.getCurrentTimeBeijing());
        map.put("segment", "测试");

        //上报埋点数据
        YibaEvent.getInstance().event(context, map);
    }

    /**
     * 获取app 版本
     */
    public String getAppVersion(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "Unknown";
        }
    }


    public void setUserProperty(Context context, String key, String cn) {
        getmFirebaseAnalytics(context).setUserProperty(key, cn);
    }

    private static FirebaseAnalytics mFirebaseAnalytics;

    private static FirebaseAnalytics getmFirebaseAnalytics(Context context) {
        if (mFirebaseAnalytics == null) {
            mFirebaseAnalytics = FirebaseAnalytics.getInstance(context.getApplicationContext());
        }
        return mFirebaseAnalytics;
    }


    public void onResume(Context context) {
        YibaAppAgent.getInstance().onResume(context);
    }

    public void onPause(Context context) {
        YibaAppAgent.getInstance().onPause(context);
    }
}
